 package com.bnzj.core.util;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author barry
 * @date 2019-01-10
 */
@Slf4j
public class EmailUtils {
    
    public static boolean sendMail(String to, String text, String subject) {
        JavaMailSender javaMailSender = SpringUtils.get(JavaMailSender.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        try {
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(SpringUtils.getProperty("spring.mail.username"));
            messageHelper.setText(text, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            log.error("", e);
            return false;
        }
    }
    /**
     * sendMail:带附件发送. <br/>
     * @author barry
     * @param to
     * @param text 正文
     * @param subject 主题，话题
     * @param file
     * @return  
     * @since JDK 1.8
     */
    public static boolean sendMail(String to, String text, String subject, File file, String fileName) {
        JavaMailSender javaMailSender = SpringUtils.get(JavaMailSender.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(SpringUtils.getProperty("spring.mail.username"));
            messageHelper.setText(text, true);
            if (null != file) {
                messageHelper.addAttachment(MimeUtility.encodeText(fileName), file);
            }
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
    }
    
    /**
     * sendMail:批量发送. <br/>
     * @author barry
     * @param to
     * @param text 正文
     * @param subject 主题，话题
     * @return  
     * @since JDK 1.8
     */
    public static boolean sendMail(String[] to, String text, String subject) {
        JavaMailSender javaMailSender = SpringUtils.get(JavaMailSender.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
        try {
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(SpringUtils.getProperty("spring.mail.username"));
            messageHelper.setText(text, true);
            javaMailSender.send(mimeMessage);
            return true;
        } catch (MessagingException e) {
            log.error("", e);
            return false;
        }
    }
    
    /**
     * sendMail:带附件的批量发送. <br/>
     * @author barry
     * @param to
     * @param text 正文
     * @param subject 主题，话题
     * @param file
     * @return  
     * @since JDK 1.8
     */
    public static boolean sendMail(String[] to, String text, String subject, File file, String fileName) {
        JavaMailSender javaMailSender = SpringUtils.get(JavaMailSender.class);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setFrom(SpringUtils.getProperty("spring.mail.username"));
            messageHelper.setText(text, true);
            if (null != file) {
                messageHelper.addAttachment(MimeUtility.encodeText(fileName), file);
            }
            javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
    }
}
